package com.cyandpos.pad.pickhangup;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.utils.MoneyUtils;

import java.util.ArrayList;
import java.util.List;



public class GoodsListAdapter extends BaseAdapter {

    private List<Goods> goodsList =new ArrayList<>(); // 数据
    private LayoutInflater inflator;
    private Context context;
    private int selectedPosition = -1;

    /**
     * @param context mainActivity
     */
    public GoodsListAdapter(Context context, TicketInfo ticketInfo) {
        this.context = context;

        for (int i=0;i<ticketInfo.getSubTickets().size();i++){
                this.goodsList.addAll(ticketInfo.getSubTickets().get(0).getGoodsList());
        }

        //this.goodsList = (List<Goods>) ticketInfo.getSubTickets().get(0).getGoodsList();
    }

    /*
    * 获得数据总数
    */
    @Override
    public int getCount() {
        return goodsList.size();
    }

    /*
     * 根据索引为position的数据
     */
    @Override
    public Object getItem(int position) {
        return goodsList.get(position);
    }

    /*
     * 根据索引值获得Item的Id
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.item_pick_hang_up_goods, null);
            viewHolder = new ViewHolder();
            viewHolder.id = (TextView) convertView.findViewById(R.id.tv_goods_position);
            viewHolder.name = (TextView) convertView.findViewById(R.id.tv_goods_name);
            viewHolder.count = (TextView) convertView.findViewById(R.id.tv_goods_count);
            viewHolder.price = (TextView) convertView.findViewById(R.id.tv_goods_price);
            viewHolder.total_money = (TextView) convertView.findViewById(R.id.tv_goods_total_money);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

//        if (selectedPosition == position) {
//            convertView.setBackgroundColor(0xFFFBD8BE);//R.color.pink
//        } else {
//            convertView.setBackgroundColor(Color.WHITE);
//        }
        setValues(position, viewHolder);
        return convertView;
    }

    private void setValues(int position, ViewHolder viewHolder) {

        Goods goods = goodsList.get(position);

        viewHolder.id.setText(String.valueOf(position + 1));
        viewHolder.count.setText(String.valueOf(goods.getSaleCount()));
        viewHolder.name.setText(goods.getName());
        viewHolder.price.setText(MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(goods.getPrice())));
        viewHolder.total_money.setText(MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(goods.getSaleMoney())));

    }

    private class ViewHolder {
        private TextView count;
        private TextView name;
        private TextView price;
        private TextView total_money;
        private TextView id;
    }
}
